<template>
  <div class="login-bg">
    <div class="login-card">
      <div class="login-header">
        <!-- <div class="login-logo">
          <img src="https://cdn.jsdelivr.net/gh/duogongneng/oss-picbed/img/logo-campus.svg" alt="logo" />
        </div> -->
        <h2>欢迎登录校园综合平台</h2>
        <p class="login-desc">让校园生活更智能、更便捷</p>
      </div>
      <form class="login-form" @submit.prevent="handleLogin">
        <div class="input-group">
          <span class="input-icon"><i class="icon icon-user"></i></span>
          <input
            type="text"
            v-model="username"
            placeholder="请输入用户名"
            :class="{ 'input-error': usernameError }"
            autocomplete="username"
          />
        </div>
        <span v-if="usernameError" class="error-message">{{ usernameError }}</span>
        <div class="input-group">
          <span class="input-icon"><i class="icon icon-lock"></i></span>
          <input
            type="password"
            v-model="password"
            placeholder="请输入密码"
            :class="{ 'input-error': passwordError }"
            autocomplete="current-password"
          />
        </div>
        <span v-if="passwordError" class="error-message">{{ passwordError }}</span>
        <div class="login-options">
          <label class="remember-me">
            <input type="checkbox" v-model="rememberMe" />
            <span class="remember-text">记住我</span>
          </label>
          <router-link to="/forgot" class="forgot-link">忘记密码？</router-link>
        </div>
        <button class="login-btn" type="submit" :disabled="isSubmitting">
          {{ isSubmitting ? '登录中...' : '登录' }}
        </button>
        <div class="login-bottom">
          没有账号？<router-link to="/register" class="register-link">立即注册</router-link>
        </div>
        <button class="admin-login-btn" type="button" @click="router.push('/login-admin')">管理员登录</button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStorage } from '@vueuse/core'

const username = ref('')
const password = ref('')
const rememberMe = ref(false)
const isSubmitting = ref(false)
const usernameError = ref('')
const passwordError = ref('')
const rememberedUser = useStorage('remember_me', null)
const router = useRouter()

if (rememberedUser.value) {
  username.value = rememberedUser.value.username
  password.value = rememberedUser.value.password
  rememberMe.value = true
}

const validateForm = () => {
  usernameError.value = username.value.trim() ? '' : '用户名不能为空'
  passwordError.value = password.value.trim() ? '' : '密码不能为空'
  if (!passwordError.value && password.value.length < 6) {
    passwordError.value = '密码长度不能少于6位'
  }
  return !usernameError.value && !passwordError.value
}

const handleLogin = async () => {
  if (!validateForm()) return
  isSubmitting.value = true
  try {
    await new Promise(resolve => setTimeout(resolve, 1200))
    localStorage.setItem('token', 'mock-token')
    if (rememberMe.value) {
      rememberedUser.value = {
        username: username.value,
        password: password.value
      }
    } else {
      rememberedUser.value = null
    }
    router.push('/main/announcements')
  } catch (e) {
    // 错误处理
  } finally {
    isSubmitting.value = false
  }
}
</script>

<style scoped>
:global(html, body, #app) {
  margin: 0;
  padding: 0;
  width: 100vw;
  height: 100vh;
  min-height: 100vh;
  background: linear-gradient(135deg, #e0e7ff 0%, #f8fafc 100%) !important;
  box-sizing: border-box;
}
.login-bg {
  min-height: 100vh;
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 0;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
}
.login-card {
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 4px 32px rgba(37,99,235,0.10);
  padding: 48px 38px 36px 38px;
  width: 100%;
  max-width: 370px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login-header {
  text-align: center;
  margin-bottom: 32px;
}
.login-logo img {
  width: 56px;
  height: 56px;
  margin-bottom: 10px;
}
.login-header h2 {
  font-size: 1.6em;
  color: #2563eb;
  margin-bottom: 8px;
  font-weight: bold;
}
.login-desc {
  color: #888;
  font-size: 1em;
  margin-bottom: 0;
}
.login-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.input-group {
  position: relative;
  display: flex;
  align-items: center;
  margin-bottom: 2px;
}
.input-icon {
  position: absolute;
  left: 12px;
  color: #b3b3b3;
  font-size: 1.1em;
}
.input-group input {
  width: 100%;
  padding: 10px 12px 10px 38px;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  font-size: 1em;
  outline: none;
  transition: border 0.2s;
  background: #f7faff;
}
.input-group input:focus {
  border: 1.5px solid #2563eb;
  background: #fff;
}
.input-error {
  border: 1.5px solid #e74c3c !important;
  background: #fff0f0 !important;
}
.error-message {
  color: #e74c3c;
  font-size: 0.95em;
  margin-bottom: 2px;
  margin-left: 2px;
}
.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 6px 0 2px 0;
}
.remember-me {
  font-size: 0.98em;
  color: #555;
  display: inline-flex;
  flex-direction: row;
  align-items: center;
  gap: 6px;
  vertical-align: middle;
  white-space: nowrap;
}
.remember-text {
  display: inline-block;
  margin-left: 2px;
}
.forgot-link {
  color: #2563eb;
  font-size: 0.98em;
  text-decoration: underline;
  transition: color 0.2s;
}
.forgot-link:hover {
  color: #1e40af;
}
.login-btn {
  width: 100%;
  background: linear-gradient(90deg, #2563eb 60%, #4f8cff 100%);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 12px 0;
  font-size: 1.1em;
  font-weight: bold;
  margin-top: 10px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s;
  box-shadow: 0 2px 8px rgba(37,99,235,0.08);
}
.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}
.login-btn:hover:not(:disabled) {
  background: linear-gradient(90deg, #1e40af 60%, #2563eb 100%);
}
.login-bottom {
  text-align: center;
  margin-top: 8px;
  color: #888;
  font-size: 0.98em;
}
.register-link {
  color: #2563eb;
  text-decoration: underline;
  margin-left: 4px;
  transition: color 0.2s;
}
.register-link:hover {
  color: #1e40af;
}
.admin-login-btn {
  width: 100%;
  background: linear-gradient(90deg, #22c55e 60%, #4ade80 100%);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 12px 0;
  font-size: 1.05em;
  font-weight: bold;
  margin-top: 8px;
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s;
  box-shadow: 0 2px 8px rgba(34,197,94,0.08);
}
.admin-login-btn:hover {
  background: linear-gradient(90deg, #16a34a 60%, #22c55e 100%);
}
@media (max-width: 500px) {
  .login-card {
    padding: 28px 8px 18px 8px;
    max-width: 98vw;
  }
}
</style>